#include <windows.h>
#include <gl/glut.h>
#include <iostream>
#include "Camera.h"

using namespace std;

extern float lod;
extern float lodDecay;
extern Camera cam;


///////////////////
//Keyboard Controls
///////////////////
bool keyStates[256] = {false}; // Create a bool for each keyboard key, set default to 'released'.

void doKeyUp(unsigned char key, int x, int y) {
	keyStates[key] = false;  // Set key as 'released'
}

void doKeyboard(unsigned char key, int x, int y) {
	keyStates[key] = true;	// Set key as 'pressed'
}

// Called each frame update-- checks if keystates are set to pressed or not.
void updateControls(float delta) {
	// Camera Translations
	if (keyStates['w'])	cam.slide(0, 0,-6*delta);
	if (keyStates['s'])	cam.slide(0, 0, 6*delta);
	if (keyStates[' '])	cam.slide(0, 4*delta, 0);
	if (keyStates['c'])	cam.slide(0,-4*delta, 0);
	if (keyStates['a'])	cam.slide(-6*delta, 0, 0);
	if (keyStates['d'])	cam.slide( 6*delta, 0, 0);

	// Camera Viewing angle and Screen Rotation
	if (keyStates['[']) { cam.setViewingAngle(cam.viewingAngle + 5 * delta); }
	if (keyStates[']']) { cam.setViewingAngle(cam.viewingAngle - 5 * delta); }
	if (keyStates[','] || keyStates['<']) { cam.setScreenRatio(cam.screenRatio + 0.5 * delta); }
	if (keyStates['.'] || keyStates['>']) { cam.setScreenRatio(cam.screenRatio - 0.5 * delta); }

	// Grass Level-of-detail
	if (keyStates['+']) { lod += 2 * delta; }
	if (keyStates['-']) { lod -= 2 * delta;
		if (lod < 0)
			lod = 0;
	}
	if (keyStates['*']) { lodDecay += 2 * delta; }
	if (keyStates['/']) { lodDecay -= 2 * delta;
		if (lodDecay < 0)
			lodDecay = 0;
	}


	// Camera rotations
	if (keyStates['q'])	cam.roll(32.0*delta);
	if (keyStates['e'])	cam.roll(-32.0*delta);
}


/////////////////
// Mouse Controls
/////////////////
float lastMouseX, lastMouseY;
bool mouseLeftPressed, mouseRightPressed;	// Mouse pressed states for motion function

void doMouse(int button, int state, int x, int y) {
	if(button == GLUT_LEFT_BUTTON)
		mouseLeftPressed = (state==GLUT_DOWN);
	else if(button == GLUT_RIGHT_BUTTON)
		mouseRightPressed = (state==GLUT_DOWN);

	lastMouseX = x;
	lastMouseY = y;
}

void doMotion(int x, int y) {
	if (mouseLeftPressed) {			// Camera pitch/yaw
		cam.pitch((lastMouseY - y) / 10);
		cam.yaw((lastMouseX - x) / 10);
	} else if (mouseRightPressed)	// Camera roll
		cam.roll((lastMouseX - x) / 10);

	lastMouseX = x;
	lastMouseY = y;
}

// Print control instructions
void printInstructions() {
	cout << "Controls:\n";
	cout << "\tw/s        = Move forward/backward\n";
	cout << "\ta/d        = Strafe left/right\n";
	cout << "\tspace/c    = Move up/down\n";
	cout << "\n";
	cout << "\tLeft click = Pitch/yaw the camera\n";
	cout << "\tRight click= Roll the camera\n";
	cout << "\tq/e        = Roll CCW/CW\n";
	cout << "\n";
	cout << "\t[/]        = Change viewing angle\n";
	cout << "\t</>        = Change aspect ration\n";
	cout << "\n";
	cout << "\t+/-        = Change grass draw distance\n";
	cout << "\t*//        = Change grass decay distance\n";
}
